from app import create_app
from app.config import config

# 初始化
app = create_app(config)

# 中间件
@app.middleware
async def simple_md(app, request, receive, send):
    # 注入到 request
    request.db = 'db'

    try:
        response = await app(request, receive, send)
        response.headers['framework'] = 'muffin'
        return response
    except RuntimeError:
        return 'Middleware Exception'


@app.on_error(TimeoutError)
async def timeout(request, error):
    return '请求错误'

if __name__ == "__main__":

    # uvicorn main:app
    import uvicorn
    uvicorn.run(app, host=config['host'], port=config['port'], log_level=config['debug'])


